const server = require('express')();
const url = require('url');
const fs = require('fs');
const ejs = require('ejs');
const mysql = require('mysql');
const expressStatic = require('express-static');
const { error } = require('console');
const port = 90;

let sql = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '123456',
    database: 'student'
});
sql.connect();

// 配置默认文件
server.get(`/`, (request, response) => {
    sql.query(`SELECT * FROM student`, (error, data) => {
        if (error) {
            console.log(error);
            response.end('error');
        } else {
            // 读取文件
            ejs.renderFile('static/index.html', { studentList: data }, (error, result) => {
                response.end(result);
            })
        }
    })
})

server.get(`/getData`, (request, response) => {
    // let id = url.parse(req.url,true).query.id;
    // sql.query(`SELECT * FROM goods`, (error, data) => {
    //     if (error) {
    //         console.log(error);
    //         response.end("error");
    //     }
    //     else {
    //         response.end(JSON.stringify(data));
    //     }
    // })
    response.end('success');
})

server.use(expressStatic(__dirname + '/static'));
server.listen(port);
console.log(`server is running at ${port}`);